package common.utils.msg;

import java.io.Serializable;

public class RespData<T> implements Serializable {

    private Integer code;
    private String message;
    private T data;

    public RespData() {
        this.code = RespSuccessMsg.SUCCESS.getCode();
        this.message = RespSuccessMsg.SUCCESS.getMessage();
    }

    private RespData(IrespMsg respMsg) {
        this.code = respMsg.getCode();
        this.message = respMsg.getMessage();
    }

    private RespData(IrespMsg respMsg, T data) {
        this.code = respMsg.getCode();
        this.message = respMsg.getMessage();
        this.data = data;
    }

    private RespData(T data) {
        this();
        this.data = data;
    }

    public static <R> RespData<R> success() {
        return new RespData<>();
    }

    public static <T> RespData<T> success(IRespSuccessMsg msg) {
        return new RespData<>(msg);
    }

    public static <T> RespData<T> success(T t) {
        RespData<T> success = success();
        success.setData(t);
        return success;
    }

    public static <T> RespData<T> success(IRespSuccessMsg msg, T t) {
        RespData<T> success = success(msg);
        success.setData(t);
        return success;
    }

    public static <T> RespData<T> fail() {
        return new RespData<>(RespFailMsg.FAIL);
    }

    public static <T> RespData<T> fail(IRespFailMsg msg) {
        return new RespData<>(msg);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return RespSuccessMsg.SUCCESS.getCode().equals(code);
    }

}
